//
//  ECMinePrivateLetterDetailCell.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/12.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMinePrivateLetterDetailCell.h"
#import "ContactDateHelper.h"

@interface ECMinePrivateLetterDetailCell()
///消息btn
@property (nonatomic, strong) UIButton *textsButton;
///头像image
@property (nonatomic, strong) UIImageView *headImageView;
///时间lab
@property (nonatomic, strong) UILabel *timeLab;

@end

@implementation ECMinePrivateLetterDetailCell

+(ECMinePrivateLetterDetailCell *)tableViewCellWithTableView:(UITableView *)tableView{
    static NSString* identifier = @"ECMinePrivateLetterDetailCell";
    ECMinePrivateLetterDetailCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell.selectionStyle = 0;
        cell = [[ECMinePrivateLetterDetailCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

-(UIButton *)textsButton{
    if (!_textsButton) {
        _textsButton = [[UIButton alloc]init];
        _textsButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        _textsButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
        _textsButton.titleLabel.numberOfLines=0;
        [_textsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.contentView addSubview:_textsButton];
    }
    return _textsButton;
}

-(UIImageView *)headImageView{
    if (!_headImageView){
        _headImageView = [[UIImageView alloc]init];
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = kProportion_Width(54);
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

-(UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [[UILabel alloc]init];
        _timeLab.textAlignment = NSTextAlignmentCenter;
        _timeLab.textColor = [UIColor colorWithHexString:@"#c6c5c6"];
        _timeLab.font = [UIFont systemFontOfSize:12.f];
        [self.contentView addSubview:_timeLab];
    }
    return _timeLab;
}

-(void)setContentOfCellWith:(CellModelFrame *)model andLastModel:(CellModelFrame *)lastModel{
    _cellModelF = model;
    
    //设置内容
    [self setContentsWithLastModel:lastModel];
    //设置位置
    [self setFrames];
}

-(void)setContentsWithLastModel:(CellModelFrame *)lastModel{
    self.backgroundColor = [UIColor clearColor];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *birthdayDate = [dateFormatter dateFromString:self.cellModelF.cellModel.createdTime];
    
    NSString *timeStr = [[ContactDateHelper sharedInstance]stringWithDate:birthdayDate];
    
    //设置时间
    self.timeLab.text = timeStr;
    
    //设置头像
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.cellModelF.cellModel.smallAvatar] placeholderImage:[UIImage imageNamed:kEduCourseImagePlaceholderName]];
    
    //设置消息内容
    [self.textsButton setTitle:self.cellModelF.cellModel.content forState:UIControlStateNormal];
    if ([self.cellModelF.cellModel.type isEqualToString:@"they"]) {
        _textsButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 5);//设置btn -margin
        UIImage *image = [self resizeImageWithIOS5Method:@"Mine_chatReciveOther" andUIEdgeInsetsMake:UIEdgeInsetsMake(30, 15, 30, 15)];
        [self.textsButton setBackgroundImage:image forState:UIControlStateNormal];
    }else{
        _textsButton.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 15);//设置btn -margin
        UIImage *iamge = [self resizeImageWithIOS5Method:@"Mine_chatSendSelf" andUIEdgeInsetsMake:UIEdgeInsetsMake(30, 10, 30, 28)];
        [self.textsButton setBackgroundImage:iamge forState:UIControlStateNormal];
    }
}

///伸缩图片后重新赋值
-(UIImage*)resizeImageWithIOS5Method:(NSString*)imageName andUIEdgeInsetsMake:(UIEdgeInsets)insert{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image resizableImageWithCapInsets:insert];
}

-(void)setFrames{
    self.timeLab.frame = self.cellModelF.timeF;
    self.headImageView.frame = self.cellModelF.headImageViewF;
    self.textsButton.frame = self.cellModelF.textF;
}

@end
